/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel.typefactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.prefabware.jmodel.JType;

/**
 * 
 * @author Stefan Isele
 * 
 */
public class JTypeProxy implements InvocationHandler {
	private Class<?> javaClass;
	private Class<? extends JType> typeClass;
	private TypeFactory typeFactory;

	private JTypeProxy(Class<?>javaClass,Class<? extends JType> typeClass, TypeFactory typeFactory) {
		this.typeClass = typeClass;
		this.javaClass=javaClass;
		this.typeFactory=typeFactory;
	}

	@SuppressWarnings("unchecked")
	public static <JTYPE extends JType> JTYPE create(Class<?>javaClass,Class<JTYPE> typeClass, TypeFactory typeFactory) {
		return (JTYPE) Proxy.newProxyInstance(javaClass.getClassLoader(),
				new Class[] { typeClass }, new JTypeProxy(javaClass,typeClass,typeFactory));
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
			
			return method.invoke(typeClass, args);
	}

	@Override
	public String toString() {
		return "AnnotationDefaults for " + this.typeClass;
	}

}